package com.example.yunhailutong.zhongtan.Adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.MessageDetailsActivity;
import com.example.yunhailutong.zhongtan.entity.HomeEntity.DataBean;
import com.example.yunhailutong.zhongtan.entity.HomeEntity.DataBean.HomeInfoBean;

import java.util.ArrayList;
import java.util.List;

public class SecondPartViewPagerAdapter extends PagerAdapter {
    private DataBean mDataBean;
    List<HomeInfoBean> mPartInfoList = new ArrayList<HomeInfoBean>();
    List<HomeInfoBean> mListViewInfo = new ArrayList<HomeInfoBean>();
    List<HomeInfoBean> mImageViewInfo = new ArrayList<HomeInfoBean>();
    private SecondPartListViewAdapter listViewAdapter;
    public SecondPartViewPagerAdapter(DataBean datalist) {
        mDataBean = datalist;

    }
    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        for (int i = 0; i < 5; i++) {
            if (i < mDataBean.getLstDjxc().size()) {
                mPartInfoList.add(mDataBean.getLstDjxc().get(i));
            } else {
                mPartInfoList.add(mPartInfoList.get(0));
            }
        }
        mImageViewInfo = mPartInfoList.subList(0, 2);
        mListViewInfo = mPartInfoList.subList(2, 5);
        //super.instantiateItem(container, position);
        View view = View.inflate(container.getContext(), R.layout.fragment_main_second_part, null);
        //将图片圆角化显示
        ImageView imageView01 = view.findViewById(R.id.second_part_Image1);
        ImageView imageView02 = view.findViewById(R.id.second_part_Image2);
        TextView textView01 = view.findViewById(R.id.second_part_tv1);
        TextView textView02 = view.findViewById(R.id.second_part_tv2);
        if (mImageViewInfo.get(0).getIm_imgPath() != null && !mImageViewInfo.get(0).getIm_imgPath().isEmpty()) {
            Glide.with(view.getContext()).load(mImageViewInfo.get(0).getIm_imgPath()).into(imageView01);
            textView01.setText(mImageViewInfo.get(0).getIm_title());
        }

        if (mImageViewInfo.get(1).getIm_imgPath() != null && !mImageViewInfo.get(1).getIm_imgPath().isEmpty()) {
            Glide.with(view.getContext()).load(mImageViewInfo.get(1).getIm_imgPath()).into(imageView02);
            textView02.setText(mImageViewInfo.get(1).getIm_title());
        }

        ListView listView = view.findViewById(R.id.second_part_listview);
        listViewAdapter = new SecondPartListViewAdapter(view.getContext(), mListViewInfo);
        listView.setAdapter(listViewAdapter);
        container.addView(view);

        //ListView Item 监听点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeInfoBean informationBean = mListViewInfo.get(position);
                Intent intent = new Intent(view.getContext(), MessageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", informationBean.getIm_content());
                bundle.putString("title", informationBean.getIm_title());
                bundle.putString("img_url", informationBean.getIm_imgPath());
                intent.putExtras(bundle);
                view.getContext().startActivity(intent);
            }
        });

        imageView01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeInfoBean informationBean = mImageViewInfo.get(0);
                Intent intent = new Intent(v.getContext(), MessageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", informationBean.getIm_content());
                bundle.putString("title", informationBean.getIm_title());
                bundle.putString("img_url", informationBean.getIm_imgPath());
                intent.putExtras(bundle);
                v.getContext().startActivity(intent);
            }
        });

        imageView02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeInfoBean informationBean = mImageViewInfo.get(1);
                Intent intent = new Intent(v.getContext(), MessageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", informationBean.getIm_content());
                bundle.putString("title", informationBean.getIm_title());
                bundle.putString("img_url", informationBean.getIm_imgPath());
                intent.putExtras(bundle);
                v.getContext().startActivity(intent);
            }
        });


        return view;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
}
